PREFIX ?=	/usr/local
BINDIR ?=	$(PREFIX)/bin
MANDIR ?=	$(PREFIX)/share/man
BINMODE ?=	0755
INSTALL ?=	install
LNKSRCS_DIRS ?=
CFLAGS ?=
COPTS ?=
LDFLAGS ?=
LDOPTS ?=

ifeq ($(filter compat,$(LNKSRCS_DIRS)),)
LNKSRCS_DIRS +=		compat
endif
LNKSRCS_compat +=	arc4random.c strlcpy.c strtonum.c

SRCS +=	$(foreach d, $(LNKSRCS_DIRS), $(LNKSRCS_$d))
OBJS =	$(patsubst %.c,%.o,$(SRCS))
VPATH =	$(shell echo $(foreach d, $(LNKSRCS_DIRS), ../$d) | sed -e 's/ /:/g')

CC ?=		cc
ifndef DEBUG
CFLAGS +=	-DNDEBUG
LDFLAGS +=	-s
endif
CFLAGS +=	-pipe -std=gnu99 -Wall -Werror-implicit-function-declaration \
		$(DEBUG) $(COPTS)
LDFLAGS +=	$(foreach l, $(LIBS), -l $l) $(LDOPTS)
DEBUG ?=	-O0 -ggdb -DDEBUG -D_DEBUG

LINTFLAGS =	-hx -g

all: $(PROG)

$(PROG): $(OBJS)
	$(CC) $(LDFLAGS) -o $@ $(OBJS)

$(OBJS): %.o: %.c
	$(CC) -c $(CFLAGS) $< -o $@

install: all
	$(INSTALL) -Dm $(BINMODE) $(PROG) $(DESTDIR)$(BINDIR)

cleandir: clean cleanlinks cleancmake

clean:
	rm -f -- $(PROG) $(OBJS)

cleanlinks:

cleancmake:
	cd $(CURDIR) && rm -f Makefile CMakeCache.txt cmake_install.cmake
	cd $(CURDIR) && rm -Rf CMakeFiles
